package cn.cloudplug.friendsrecommend;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;
import cn.cloudplug.friendsrecommend.R;
import cn.cloudplug.friendsrecommend.entity.Member;
import cn.cloudplug.friendsrecommend.entity.Team;
import cn.cloudplug.friendsrecommend.util.Dp2px;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnSwipeListener;

public class MyTeamInfoActivity extends Activity implements OnClickListener {
	private SwipeMenuListView lv_swip;
	private List<Member> members;
	private MyMemberAdapter adapter;
	private Team team;
	private Dialog memberDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_team_info);
		getTestData();
		setView();
	}

	private void getTestData() {
		// TODO Auto-generated method stub
		Intent intent = getIntent();
		team = (Team) intent.getSerializableExtra("team");
		members = new ArrayList<Member>();
		for (int i = 0; i < team.getTeamMemberCount(); i++) {
			Member m = new Member(i, i, "张" + i + "山",
					(int) (Math.random() * 10 * i) + "", "2015年4月1日");
			members.add(m);
		}
	}

	private void setView() {
		
		((TextView)findViewById(R.id.tv_team_name)).setText(team.getTeamName());
		int group=team.getTeamGroupID();
		String str="";
		if(group==1){
			str="篮球";
		}else if(group==2){
			str="足球";
		}else if(group==3){
			str="赛车";
		}
		((TextView)findViewById(R.id.tv_team_group)).setText(str);
		((TextView)findViewById(R.id.tv_team_remarks)).setText(team.getTeamRemarks());
		((TextView)findViewById(R.id.tv_team_create_date)).setText(team.getTeamCreateDate());
		((TextView)findViewById(R.id.tv_team_member_count)).setText("共"+team.getTeamMemberCount()+"人");
		
		
		
		ViewGroup vp=(ViewGroup) findViewById(R.id.title);
		vp.findViewById(R.id.iv_home).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		findViewById(R.id.btn_invite_friend).setOnClickListener(this);
		findViewById(R.id.iv_team_name_btn).setOnClickListener(this);
		findViewById(R.id.iv_team_group_btn).setOnClickListener(this);
		findViewById(R.id.iv_team_remarks_btn).setOnClickListener(this);
		findViewById(R.id.tv_team_create_date_btn).setOnClickListener(this);
		findViewById(R.id.iv_add_team_member).setOnClickListener(this);
		
		lv_swip = (SwipeMenuListView) findViewById(R.id.lv_swip);
		adapter = new MyMemberAdapter();
		
		SwipeMenuCreator creator = new SwipeMenuCreator() {
			@Override
			public void create(SwipeMenu menu) {
				
				SwipeMenuItem phoneItem = new SwipeMenuItem(MyTeamInfoActivity.this);
				phoneItem.setBackground(new ColorDrawable(Color.rgb(16,38, 79)));
				phoneItem.setWidth(Dp2px.getPx(120, MyTeamInfoActivity.this));
				phoneItem.setIcon(R.drawable.cloud_phone_call_icon);
				menu.addMenuItem(phoneItem);
				
				SwipeMenuItem deleteItem = new SwipeMenuItem(MyTeamInfoActivity.this);
				deleteItem.setBackground(new ColorDrawable(Color.rgb(16,38, 79)));
				deleteItem.setWidth(Dp2px.getPx(120, MyTeamInfoActivity.this));
				deleteItem.setIcon(R.drawable.cloud_close);
				menu.addMenuItem(deleteItem);
			}
		};
		lv_swip.setMenuCreator(creator);
		lv_swip.setAdapter(adapter);
		setListViewHeightBasedOnChildren(lv_swip);
		lv_swip.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(int position, SwipeMenu menu,int index) {
				switch (index) {
				case 0:
					Toast.makeText(MyTeamInfoActivity.this, "打电话", 0).show();
					break;
				case 1:
					members.remove(position);
					adapter.notifyDataSetChanged();
					break;
				}
				return false;
			}
		});
		lv_swip.setOnSwipeListener(new OnSwipeListener() {
			public void onSwipeStart(int position) {
			}
			public void onSwipeEnd(int position) {
			}
		});
		lv_swip.setOnItemLongClickListener(new OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(MyTeamInfoActivity.this,position + " long click", 0).show();
				return false;
			}
		});
		lv_swip.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					final int position, long id) {
				view=LayoutInflater.from(MyTeamInfoActivity.this).inflate(R.layout.member_info_dialog, null);
				view.findViewById(R.id.iv_close).setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						memberDialog.dismiss();
					}
				});
				view.findViewById(R.id.tv_delete_member).setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						members.remove(position);
						memberDialog.dismiss();
						adapter.notifyDataSetChanged();
					}
				});
				Builder b=new Builder(MyTeamInfoActivity.this).setView(view);
				memberDialog=b.create();memberDialog.show();
			}
		});

	}

	class MyMemberAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return members.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return members.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder vh = null;
			if (convertView == null) {
				convertView = View.inflate(getApplicationContext(),
						R.layout.list_item_row, null);
				vh = new ViewHolder();
				vh.iv_photo = (ImageView) convertView
						.findViewById(R.id.iv_photo);
				vh.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
				convertView.setTag(vh);
			}
			vh = (ViewHolder) convertView.getTag();
			final Member m = members.get(position);
			vh.iv_photo.setImageResource(R.drawable.ic_launcher);
			vh.tv_name.setText(m.getName());
			return convertView;
		}

		class ViewHolder {
			ImageView iv_photo;
			TextView tv_name;
		}

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.iv_add_team_member:
		case R.id.btn_invite_friend:
			Toast.makeText(MyTeamInfoActivity.this, "邀请好友", 0).show();
			break;
		case R.id.iv_team_name_btn:
			Toast.makeText(MyTeamInfoActivity.this, "修改团队名称", 0).show();
			break;
		case R.id.iv_team_group_btn:
			Toast.makeText(MyTeamInfoActivity.this, "修改团队类型", 0).show();
			break;
		case R.id.iv_team_remarks_btn:
			Toast.makeText(MyTeamInfoActivity.this, "修改团队备注", 0).show();
			break;
		case R.id.tv_team_create_date_btn:
			Toast.makeText(MyTeamInfoActivity.this, "修改团队组建日期", 0).show();
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode == 2) {
			if (resultCode == RESULT_OK) {
				String value = data.getStringExtra("value");
				team.setTeamName(value);
				((TextView) findViewById(R.id.tv_team_name)).setText(value);
			}
		}
		if (requestCode == 3) {
			if (resultCode == RESULT_OK) {
				String value = data.getStringExtra("value");
				team.setTeamName(value);
				((TextView) findViewById(R.id.tv_team_group)).setText(value);
			}
		}
		if (requestCode == 4) {
			if (resultCode == RESULT_OK) {
				String value = data.getStringExtra("value");
				team.setTeamName(value);
				((TextView) findViewById(R.id.tv_team_remarks)).setText(value);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	

    public void setListViewHeightBasedOnChildren(SwipeMenuListView listView) {    
        ListAdapter listAdapter = listView.getAdapter();     
        if (listAdapter == null) {    
            return;    
        }    
      
        int totalHeight = 0;    
        for (int i = 0; i < listAdapter.getCount(); i++) {    
            View listItem = listAdapter.getView(i, null, listView);    
            listItem.measure(0, 0);    
            totalHeight += listItem.getMeasuredHeight();    
        }    
      
        ViewGroup.LayoutParams params = listView.getLayoutParams();    
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));    
        ((MarginLayoutParams)params).setMargins(15, 15, 15, 15);  
        listView.setLayoutParams(params);    

    }   
}
